
new Vue({
	el:'#nearbyPage',
	data(){

		return {
			courseId: '',
			courseItems: [
				{ 'name': 'Wonders--启蒙班', id: 10001 ,'info': '适合英语零基础的孩子'},
				{ 'name': 'Wonders--基础班', id: 10002 ,'info': '适合已有一年左右英语学习基础的孩子'},
				{ 'name': 'Wonders--进阶班', id: 10003 ,'info': '适合已有两年及以上英语学习基础的孩子'},
			],


			nearbyList:[],
			
			changeLocation: '',
			showSearch: true

		}
	},
	methods:{
		getUrlData(dataName){
			function getUrlVars(){ 
        var vars = {}, hash; 
        var href = window.location.href.split('#')[0];
        var hashes = href.slice(href.indexOf('?')+1).split('&'); 
        for(var i = 0; i < hashes.length; i++) { 
            hash = hashes[i].split('=');
            vars[hash[0]] = hash[1]?hash[1]:'';
        } 
        return vars; 
			} 
			var params = getUrlVars(); 
			
			for(var key in params){
					if(key == dataName){ 
							return params[key];
					}
			};
			return '';
		},
		getValue(id,key){
			var courseItems = this.courseItems;
			for (var i = 0; i < courseItems.length;i++){
				if (courseItems[i].id == id){
					return courseItems[i][key];
				}
			}
		},
		getDistance(number){
			if(number>1000){
				return (parseInt(number)/1000).toFixed(1)+'km';
			}else if(number==0){
				return '小于50m';
			}else{
				return parseInt(number)+'m';
			}
			
		},
		
		goDetail(groupOrderId){
			location.href = 'group.html?groupOrderId='+groupOrderId + '&stranger=1';
		},
		nearbySearch(latlng){
			var self = this;
			var aa = aa;

			$.ajax({
				url: 'https://api.svenglish.cn/api/order/nearby?lat='+ latlng.lat +'&lon=' + latlng.lng + (this.courseId ? '&courseId='+this.courseId : ''),
				type: 'GET',
				success:function(data){
					if (data.code == 1) {
						self.nearbyList = data.data;
						console.log(self.nearbyList);
					}
				}
			});	


		},
		
  },
  watch:{
    courseId:function(val){
			this.nearbySearch(this.changeLocation.latlng);
		}
  },
	mounted() {

		// this.orderSearch();

		var self = this;

		window.addEventListener('message', function(event) {
			// 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
			var loc = event.data;
			if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
				self.changeLocation = loc;

				console.log(self.changeLocation);

				localStorage.setItem('changeLocation',JSON.stringify(self.changeLocation))

				self.nearbySearch(self.changeLocation.latlng);

				setTimeout(function(){
					self.showSearch = false;
				},300);
			}
		}, false);

		var changeLocation = localStorage.getItem('changeLocation');
		if(changeLocation){
			this.changeLocation = JSON.parse(changeLocation);
			this.showSearch = false;
			this.nearbySearch(this.changeLocation.latlng);
		}
		
	}
});



